package com.application.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.application.bean.PageContion;
import com.application.bean.ResponceResult;
import com.application.pojo.Records;
import com.application.pojo.User;
import com.application.pojo.student;
import com.application.service.Bsm;
import com.google.gson.Gson;
@RestController
public class DateControl {
	@Autowired
	Bsm bsm;
	@RequestMapping(value="/form/select",method=RequestMethod.POST)
	public @ResponseBody ResponceResult  select(HttpServletRequest  r,ResponceResult responceResult,PageContion pageContion,String stuName){
		System.err.println(pageContion.getLimit());
		try {
			List list = bsm.selectStudent(pageContion);
			responceResult.setResults(bsm.countStudent());
			responceResult.setRows(list);
		} catch (Exception e) {
			// TODO: handle exception
			responceResult.setError(e.getMessage());
			responceResult.setHasError(true);
		}
		return responceResult;
	}
	@RequestMapping(value="/form/add",method=RequestMethod.POST)
	public ResponceResult  add(HttpServletRequest  r,ResponceResult responceResult,PageContion pageContion,student s){
		try {
			boolean b = bsm.saveStudent(s);
			System.err.println(b);
			responceResult.setHasError(!b);
		} catch (Exception e) {
			// TODO: handle exception
			responceResult.setError(e.getMessage());
			responceResult.setHasError(true);
		}
		return responceResult;
	}
	
	@RequestMapping(value="/form/del")
	public ResponceResult del(ResponceResult responceResult,@RequestParam(name="ids[]") Integer[] ids){
		boolean b = false;
		try {
			for (Integer id : ids) {
				 b = bsm.delStudent(id);
			}
			System.err.println(b);
			responceResult.setHasError(!b);
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println(e.getMessage());
			responceResult.setError(e.getMessage());
			responceResult.setHasError(true);
		}
		return responceResult;
	}
	@RequestMapping(value="/form/put")
	public ResponceResult put(ResponceResult responceResult, String records){
		boolean b = false;
		try {
			System.err.println(records);
			Gson g = new Gson();
			List<Records> list = g.fromJson(records, List.class);
			responceResult.setHasError(b);
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println(e.getMessage());
			responceResult.setError(e.getMessage());
			responceResult.setHasError(true);
		}
		return responceResult;
	}
	@RequestMapping(value="/form/edit")
	public ResponceResult edit(ResponceResult responceResult,student s){
		boolean b = false;
		try {
			b = bsm.editStudent(s);
			System.err.println(b);
			responceResult.setHasError(!b);
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println(e.getMessage());
			responceResult.setError(e.getMessage());
			responceResult.setHasError(true);
		}
		return responceResult;
	}
	@RequestMapping(value="permit/creatUser")
	public String creatUser(@RequestBody String userMsg){
		System.err.println(userMsg);
		Gson g = new Gson();
		Map<String, String> map =  g.fromJson(userMsg, Map.class);
		System.err.println(map);
		bsm.saveUser(map);
		return "{\"success\":\"操作成功\"}";
	}
	
	/*
	 @RequestMapping(value="findUserMsg",method=RequestMethod.POST)
	public @ResponseBody User findUserMsgByUserName(@RequestBody  String username){
		Gson g = new Gson();
		Map<String, String> map =  g.fromJson(username, Map.class);
		System.err.println(map);
		
		return bsm.selectUserByName(map.get("name"));
	}
	@RequestMapping(value="updateUserMsg",method=RequestMethod.POST)
	public @ResponseBody String updateUserMsgByUserName(@RequestBody  String msg){
		Gson g = new Gson();
		Map<String, String> map =  g.fromJson(msg, Map.class);
		System.err.println(map);
		User u = new User();
		u.setPassword(map.get("password"));
		u.setSex(map.get("sex"));
		u.setUserName(map.get("email"));
		bsm.updateUserByName(u);
		return "{\"success\":\"操作成功\"}";
	}
	 * /
	 */
}
